import Vue from "vue";
//引入router
import VueRouter from 'vue-router'
// 先存储默认底层的push
const originPush = VueRouter.prototype.push
    // 覆盖原型链的push
VueRouter.prototype.push = function(location, resolve, reject) {
    // this:路由实例对象

    // 判断用户有没有传后面两个可选参数
    if (resolve || reject) {
        return originPush.call(this, location, resolve, reject)
    } else { // 用户只传了第一个参数
        /* 
        默认底层： catch()方法代码  throw err : 抛出异常
        吞掉报错原理： 重写catch()方法,把默认底层的 throw err给去掉，就不会抛出异常
        */
        return originPush.call(this, location).catch(err => {
            //  throw err
        })
    }
}
Vue.use(VueRouter)

//配置路由
const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () =>
            import ('@/views/home/HomeViews.vue'),
        //是否显示底部导航的依据
        meta: { isShowTabbar: true },
        children: [{
            path: 'searchPopup',
            name: 'searchPopup',
            component: () =>
                import ('@/views/home/searchPopup.vue'),
            //是否显示底部导航的依据
            meta: { isShowTabbar: false },
        }]

    },
    {
        path: '/topic', //专题
        name: 'topic',
        component: () =>
            import ('@/views/topic.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/category', //分类
        name: 'category',
        component: () =>
            import ('@/views/category.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/ModificationAddress', //购物车
        name: 'ModificationAddress',
        component: () =>
            import ('@/views/ModificationAddress.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/productDetail', //详情
        name: 'productDetail',
        component: () =>
            import ('@/views/productDetail.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/cart', //购物车
        name: 'cart',
        component: () =>
            import ('@/views/cart.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/mine', //我的
        name: 'mine',
        component: () =>
            import ('@/views/mine.vue'),
        meta: { isShowTabbar: true }
    },
    {
        path: '/details', //我的
        name: 'details',
        component: () =>
            import ('@/views/details.vue'),
        meta: { isShowTabbar: false }
    },
    {
        path: '/brand', //brand
        name: 'brand',
        component: () =>
            import ('@/views/brand/brand.vue'),
        meta: { isShowTabbar: false }
    },
    {
        path: '/channel', //导航
        name: 'channel',
        component: () =>
            import ('@/views/channel.vue'),
        meta: { isShowTabbar: false }
    }

]

//创建路由对象
const router = new VueRouter({
    routes,
    mode: 'history'
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
        // 有token就表示已经登录
        // 想要进入购物车页面，必须有登录标识token
        let token = localStorage.getItem('token')
        if (to.path == '/cart') {
            // 此时必须要有token
            if (token) {
                next(); // next()去到to所对应的路由界面
            } else {
                Vue.prototype.$toast('请先登录');
                // 定时器
                setTimeout(() => {
                    next("/mine"); // 强制去到"/mine"所对应的路由界面
                }, 1000);
            }
        } else {
            // 如果不是去往购物车的路由，则直接通过守卫，去到to所对应的路由界面
            next()
        }
    })
    //导出
export default router